#include "sl_cmt.h"
#include "iodefine.h"
#include "sl_types.h"

volatile struct st_cmt0 *sl_cmt_get(int channel)
{
	volatile struct st_cmt0 *pcmt = NULL;
	if (!channel)
		pcmt = &CMT0;
	else if (channel == 1)
		pcmt = &CMT1;

	return pcmt;
}

void sl_cmt_reset(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
	{
		sl_cmt_stop_timer(pcmt);
		pcmt->CMCSR.WORD = 0;
		pcmt->CMCNT = 0;
	}
}

void sl_cmt_start_timer(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
	{
		if (pcmt == &CMT0)
			CMT.CMSTR.BIT.STR0 = 1;
		else
			CMT.CMSTR.BIT.STR1 = 1;
	}
}

void sl_cmt_stop_timer(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
	{
		if (pcmt == &CMT0)
			CMT.CMSTR.BIT.STR0 = 0;
		else
			CMT.CMSTR.BIT.STR1 = 0;
	}
}

void sl_cmt_match_flag(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
		pcmt->CMCSR.BIT.CMF = 0;
}

void sl_cmt_clear_counter(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
		pcmt->CMCNT = 0;
}

void sl_cmt_enable_irq(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
		pcmt->CMCSR.BIT.CMIE = 1;
}

void sl_cmt_disable_irq(volatile struct st_cmt0 *pcmt)
{
	if (pcmt)
		pcmt->CMCSR.BIT.CMIE = 0;
}


void sl_cmt_set_slock(volatile struct st_cmt0 *pcmt,enum ClockSelection cs)
{
	if (pcmt)
		pcmt->CMCSR.BIT.CKS = cs&3;
}
void sl_cmt_set_timer_interval(volatile struct st_cmt0 *pcmt,WORD interval){
	if (pcmt)
		pcmt->CMCOR = interval;
}

